﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TcpSyncIMServer
{
    public class Listener
    {
        private Thread thread;
        private TcpListener tcpListener;
        public volatile bool lisenerRun = true;
        public event EventHandler<AddMessageEventArgs> OnAddMessage;
        public event EventHandler<AddMessageEventArgs> OnIpRemod;

        public Listener()
        {

        }

        /// <summary>
        /// 
        /// </summary>
        public void StartLisener()
        {
            thread = new Thread(new ThreadStart(Listen));
            thread.Start();
        }

        /// <summary>
        /// 
        /// </summary>
        public void Stop() { 
           tcpListener.Stop();
            thread.Abort();
        }

        /// <summary>
        /// 
        /// </summary>
        private void Listen()
        {
            try
            {
                IPAddress iPAddress = new IPAddress(Dns.GetHostByName(Dns.GetHostName()).AddressList[0].Address);
                IPEndPoint iPEndPoint = new IPEndPoint(iPAddress, 5656);
                tcpListener = new TcpListener(iPEndPoint);
                tcpListener.Start();
                while (lisenerRun)
                {
                    Socket socket = tcpListener.AcceptSocket();
                    string remote = socket.RemoteEndPoint.ToString();
                    byte[] buffer = new byte[1024];
                    int i = socket.Receive(buffer);
                    string msg;
                    string str = System.Text.Encoding.UTF8.GetString(buffer);
                    if (str.Substring(0, 1) == "1")
                    {
                        string str_ = "欢迎登录";
                        TcpClient tcpClient = new TcpClient(((IPEndPoint)socket.RemoteEndPoint).Address.ToString(), 5657);
                        NetworkStream networkStream = tcpClient.GetStream();
                        byte[] bytes = new byte[1024];
                        networkStream.Write(buffer, 0, bytes.Length);
                        networkStream.Close();
                        msg = "<" + remote + ">" + "上线";
                        AddMessageEventArgs addMessageEventArgs = new AddMessageEventArgs();
                        addMessageEventArgs.Message = msg;
                        OnAddMessage(this, addMessageEventArgs);
                    }
                    else if (str.Substring(0, 1) == "0")
                    {
                        msg = "<" + remote + ">" + "断开";
                        AddMessageEventArgs addMessageEventArgs = new AddMessageEventArgs();
                        addMessageEventArgs.Message = remote.ToString();
                        OnIpRemod(this, addMessageEventArgs);
                        AddMessageEventArgs addMessageEventArgs1 = new AddMessageEventArgs();
                        addMessageEventArgs1.Message = msg;
                        OnAddMessage(this, addMessageEventArgs1);
                    }
                    else
                    {
                        msg = "<" + remote + ">" + System.Text.UTF8Encoding.UTF8.GetString(buffer);
                        AddMessageEventArgs addMessageEventArgs = new AddMessageEventArgs();
                        addMessageEventArgs.Message = msg;
                        OnAddMessage(this, addMessageEventArgs);
                    }
                }
            }
            catch (System.Security.SecurityException)
            {
                MessageBox.Show("防火墙禁止连接");
            }
            catch (Exception e)
            {

            }
        }
    }
}
